package my.study.java.lang.reflect.Proxy.examples;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.ArrayList;
import java.util.List;

public class Introduction {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		ArrayList<Integer> i = new ArrayList<Integer>();
		i.add(10);
		i.add(11);
		@SuppressWarnings("unchecked")
		List<Integer> j = (List<Integer>) Proxy.newProxyInstance(
				ArrayList.class.getClassLoader(), new Class[] { List.class },
				new TraceHandler(new ArrayList<Integer>()));
		j.toString();
		j.add(12);
		System.out.println(j.toString());
		my.study.java.lang.reflect.Method.examples.Introduction.printMethod(j
				.getClass());
	}

}

class TraceHandler implements InvocationHandler {
	private Object target;

	public TraceHandler(Object t) {
		target = t;
	}

	public Object invoke(Object proxy, Method m, Object[] args)
			throws Throwable {
		System.out.print(target);
		System.out.print("." + m.getName() + "(");
		if (args != null) {
			for (int i = 0; i < args.length; i++) {
				 System.out.print(args[i]);
				 if (i < args.length - 1)
				 System.out.print(", ");
			}
		}
		System.out.println(")");
		return m.invoke(target, args);
	}

}